/*
 * Copyright (c) 2018, ZeeSec Technologies. All rights reserved.
 * ZeeSec PROPRIETARY/CONFIDENTIAL.
 */
package springtest.mail;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.mail.internet.MimeMessage;

import org.junit.Test;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

/**
 * @author chenzhiqiang
 * @date 2018-08-09 10:25:13
 */

public class TestMail {
    public static void main(String[] args) throws Exception {
        JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
        senderImpl.setDefaultEncoding("UTF-8");
        senderImpl.setHost("smtp.exmail.qq.com");
        senderImpl.setPort(25);
        senderImpl.setUsername("chenzhiqiang@zeesec.cn");
        senderImpl.setPassword("ax3knVYKAxAV38kR");
        // SimpleMailMessage message = new SimpleMailMessage();
        // message.setFrom("cc_109831@163.com");// 发送者
        // message.setTo("cc_109831@163.com");// 接受者
        // message.setSubject("测试");
        // message.setText("测试的内容");
        // 压缩文件发送附件
        MimeMessage mimeMessage = senderImpl.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        messageHelper.setFrom("chenzhiqiang@zeesec.cn");
        messageHelper.setTo("cc_109831@163.com");// 接受者
        messageHelper.setSubject("绿色可以使用");
        messageHelper.setText("切记不要重复投稿，一定要检查删除重复，因为重复投稿非常容易进垃圾箱");
        File file = new File("D:\\oo.zip");
        messageHelper.addAttachment(file.getName(), file);
        senderImpl.send(mimeMessage);
    }

    @Test
    public void test() {
        compress("D:\\wkhtmltopdf.pdf", "D:\\oo.zip");
    }

    /**
     * s 压缩文件
     * 
     * @param srcFilePath
     *            压缩源路径
     * @param destFilePath
     *            压缩目的路径
     */
    public static void compress(String srcFilePath, String destFilePath) {
        //
        File src = new File(srcFilePath);

        if (!src.exists()) {
            throw new RuntimeException(srcFilePath + "不存在");
        }
        File zipFile = new File(destFilePath);

        try {

            FileOutputStream fos = new FileOutputStream(zipFile);
            ZipOutputStream zos = new ZipOutputStream(fos);
            String baseDir = "";
            compressbyType(src, zos, baseDir);
            zos.close();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }
    }

    /**
     * 按照原路径的类型就行压缩。文件路径直接把文件压缩，
     * 
     * @param src
     * @param zos
     * @param baseDir
     */
    private static void compressbyType(File src, ZipOutputStream zos, String baseDir) {

        if (!src.exists())
            return;
        System.out.println("压缩路径" + baseDir + src.getName());
        // 判断文件是否是文件，如果是文件调用compressFile方法,如果是路径，则调用compressDir方法；
        if (src.isFile()) {
            // src是文件，调用此方法
            compressFile(src, zos, baseDir);

        } else if (src.isDirectory()) {
            // src是文件夹，调用此方法
            compressDir(src, zos, baseDir);

        }

    }

    /**
     * 压缩文件
     */
    private static void compressFile(File file, ZipOutputStream zos, String baseDir) {
        if (!file.exists())
            return;
        try {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
            ZipEntry entry = new ZipEntry(baseDir + file.getName());
            zos.putNextEntry(entry);
            int count;
            byte[] buf = new byte[1024];
            while ((count = bis.read(buf)) != -1) {
                zos.write(buf, 0, count);
            }
            bis.close();

        } catch (Exception e) {
            // TODO: handle exception
        }
    }

    /**
     * 压缩文件夹
     */
    private static void compressDir(File dir, ZipOutputStream zos, String baseDir) {
        if (!dir.exists())
            return;
        File[] files = dir.listFiles();
        if (files.length == 0) {
            try {
                zos.putNextEntry(new ZipEntry(baseDir + dir.getName() + File.separator));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        for (File file : files) {
            compressbyType(file, zos, baseDir + dir.getName() + File.separator);
        }
    }
}
